package com.sgmw.sales.tool.kong.sign;

import java.util.ArrayList;
import java.util.List;

import com.sgmw.sales.tool.kong.sign.headers.AuthorizationHeader;
import com.sgmw.sales.tool.kong.sign.headers.RequestHeader;
import com.sgmw.sales.tool.kong.sign.headers.SignHeaderRequestLine;
import com.sgmw.sales.tool.kong.sign.utils.HmacSha256Util;

public abstract class AbstractAuthroizationGenerator implements AuthroizationGenerator {
	protected String username;
	protected String secret;

//	public static final String authorization = "Authorization: hmac username=\"USERNAME\", algorithm=\"hmac-sha256\", headers=\"HEADERS\", signature=\"SIGNATURE\"";
	public static final String authorization = "hmac username=\"USERNAME\", algorithm=\"hmac-sha256\", headers=\"HEADERS\", signature=\"SIGNATURE\"";
	
	public AbstractAuthroizationGenerator( String username, String secret) {
		this.secret = secret;
		this.username = username;
	}

	@Override
	public List<RequestHeader> genAuthroziationHeaders() {
		// 获取签名的请求头
		List<RequestHeader> signHeaders = decideSignHeaders();
		// 拼接签名请求头
		String signContent = getSignContent(signHeaders);
		// 生成签名请求头
		List<RequestHeader> headers = getAuthroziationHeaders(signHeaders, signContent);
		return headers;
	}

	private List<RequestHeader> getAuthroziationHeaders(List<RequestHeader> signHeaders, String signContent) {
		List<RequestHeader> authroziationHeaders = new ArrayList<>();
		//拼接Authroziation中的headers中的
		StringBuilder headersContent = new StringBuilder();
		for (int i = 0; i < signHeaders.size(); i++) {
			if (i == 0) {
				headersContent.append(signHeaders.get(i).getName());
			} else {
				headersContent.append(" ").append(signHeaders.get(i).getName());
			}
			
			if(!SignHeaderRequestLine.class.equals(signHeaders.get(i).getClass())) {
				authroziationHeaders.add(signHeaders.get(i));
			}
		}
		String authorizationString = authorization.replace("USERNAME", username).replace("HEADERS",headersContent).replace("SIGNATURE", signContent);
		AuthorizationHeader authorizationHeader = new AuthorizationHeader(authorizationString);
		authroziationHeaders.add(authorizationHeader);
		return authroziationHeaders;
	}
	
	/**
	 * 对请求行和请求头进行签名，格式：
	 * xx: xxx
	 * @param signHeaders
	 * @return
	 */
	private String getSignContent(List<RequestHeader> signHeaders) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < signHeaders.size(); i++) {
			String signString = signHeaders.get(i).toString();
			if (SignHeaderRequestLine.class.equals(signHeaders.get(i).getClass())) {
				signString = signHeaders.get(i).getValue();
			}
			if (i == 0) {
				sb.append(signString);
			} else {
				sb.append("\n").append(signString);
			}
		}
		String signContent = sb.toString();
		System.out.println("签名前的内容：\n" + signContent);
		String signature = "";
		try {
//			signature = new String(Base64.getEncoder().encode(HmacSha256Util.signatureReturnBytes(signContent, secret)),"US-ASCII");
			sun.misc.BASE64Encoder base64 = new sun.misc.BASE64Encoder();
			signature = base64.encode(HmacSha256Util.signatureReturnBytes(signContent, secret));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return signature;
	}

	protected abstract List<RequestHeader> decideSignHeaders();

}
